<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>pg_restore</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="PostgreSQL Client Applications"
HREF="reference-client.html"><LINK
REL="PREVIOUS"
TITLE="pg_dumpall"
HREF="app-pg-dumpall.html"><LINK
REL="NEXT"
TITLE="psql"
HREF="app-psql.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="pg_dumpall"
HREF="app-pg-dumpall.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="reference-client.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="psql"
HREF="app-psql.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="APP-PGRESTORE"
></A
>pg_restore</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN79628"
></A
><H2
>Name</H2
>pg_restore&nbsp;--&nbsp;   restore a <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> database from an
   archive file created by <SPAN
CLASS="APPLICATION"
>pg_dump</SPAN
>
  </DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN79635"
></A
><H2
>Synopsis</H2
><P
><TT
CLASS="COMMAND"
>pg_restore</TT
> [<TT
CLASS="REPLACEABLE"
><I
>connection-option</I
></TT
>...] [<TT
CLASS="REPLACEABLE"
><I
>option</I
></TT
>...] [<TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
>]</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="APP-PGRESTORE-DESCRIPTION"
></A
><H2
>Description</H2
><P
>   <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> is a utility for restoring a
   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> database from an archive
   created by <A
HREF="app-pgdump.html"
>pg_dump</A
> in one of the non-plain-text
   formats.  It will issue the commands necessary to reconstruct the
   database to the state it was in at the time it was saved.  The
   archive files also allow <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> to
   be selective about what is restored, or even to reorder the items
   prior to being restored. The archive files are designed to be
   portable across architectures.
  </P
><P
>   <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> can operate in two modes.
   If a database name is specified, <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
>
   connects to that database and restores archive contents directly into
   the database.  Otherwise, a script containing the SQL
   commands necessary to rebuild the database is created and written
   to a file or standard output.  This script output is equivalent to
   the plain text output format of <SPAN
CLASS="APPLICATION"
>pg_dump</SPAN
>.
   Some of the options controlling the output are therefore analogous to
   <SPAN
CLASS="APPLICATION"
>pg_dump</SPAN
> options.
  </P
><P
>   Obviously, <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> cannot restore information
   that is not present in the archive file.  For instance, if the
   archive was made using the <SPAN
CLASS="QUOTE"
>"dump data as
   <TT
CLASS="COMMAND"
>INSERT</TT
> commands"</SPAN
> option,
   <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> will not be able to load the data
   using <TT
CLASS="COMMAND"
>COPY</TT
> statements.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="APP-PGRESTORE-OPTIONS"
></A
><H2
>Options</H2
><P
>    <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> accepts the following command
    line arguments.

    <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></DT
><DD
><P
>       Specifies the location of the archive file (or directory, for a
       directory-format archive) to be restored.
       If not specified, the standard input is used.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-a</TT
><BR><TT
CLASS="OPTION"
>--data-only</TT
></DT
><DD
><P
>        Restore only the data, not the schema (data definitions).
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-c</TT
><BR><TT
CLASS="OPTION"
>--clean</TT
></DT
><DD
><P
>        Clean (drop) database objects before recreating them.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-C</TT
><BR><TT
CLASS="OPTION"
>--create</TT
></DT
><DD
><P
>        Create the database before restoring into it.  (When this
        option is used, the database named with <TT
CLASS="OPTION"
>-d</TT
> is
        used only to issue the initial <TT
CLASS="COMMAND"
>CREATE DATABASE</TT
>
        command.  All data is restored into the database name that
        appears in the archive.)
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-d <TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--dbname=<TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
></TT
></DT
><DD
><P
>        Connect to database <TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
> and restore directly
        into the database.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-e</TT
><BR><TT
CLASS="OPTION"
>--exit-on-error</TT
></DT
><DD
><P
>        Exit if an error is encountered while sending SQL commands to
        the database. The default is to continue and to display a count of
        errors at the end of the restoration.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-f <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--file=<TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></TT
></DT
><DD
><P
>        Specify output file for generated script, or for the listing
        when used with <TT
CLASS="OPTION"
>-l</TT
>. Default is the standard
        output.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-F <TT
CLASS="REPLACEABLE"
><I
>format</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--format=<TT
CLASS="REPLACEABLE"
><I
>format</I
></TT
></TT
></DT
><DD
><P
>        Specify format of the archive.  It is not necessary to specify
        the format, since <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> will
        determine the format automatically. If specified, it can be
        one of the following:

       <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>c</TT
><BR><TT
CLASS="LITERAL"
>custom</TT
></DT
><DD
><P
>           The archive is in the custom format of
           <SPAN
CLASS="APPLICATION"
>pg_dump</SPAN
>.
          </P
></DD
><DT
><TT
CLASS="LITERAL"
>d</TT
><BR><TT
CLASS="LITERAL"
>directory</TT
></DT
><DD
><P
>           The archive is a directory archive.
          </P
></DD
><DT
><TT
CLASS="LITERAL"
>t</TT
><BR><TT
CLASS="LITERAL"
>tar</TT
></DT
><DD
><P
>           The archive is a <TT
CLASS="COMMAND"
>tar</TT
> archive.
          </P
></DD
></DL
></DIV
><P></P
></DD
><DT
><TT
CLASS="OPTION"
>-i</TT
><BR><TT
CLASS="OPTION"
>--ignore-version</TT
></DT
><DD
><P
>        A deprecated option that is now ignored.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-I <TT
CLASS="REPLACEABLE"
><I
>index</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--index=<TT
CLASS="REPLACEABLE"
><I
>index</I
></TT
></TT
></DT
><DD
><P
>        Restore definition of named index only.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-j <TT
CLASS="REPLACEABLE"
><I
>number-of-jobs</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--jobs=<TT
CLASS="REPLACEABLE"
><I
>number-of-jobs</I
></TT
></TT
></DT
><DD
><P
>        Run the most time-consuming parts
        of <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> &mdash; those which load data,
        create indexes, or create constraints &mdash; using multiple
        concurrent jobs.  This option can dramatically reduce the time
        to restore a large database to a server running on a
        multiprocessor machine.
       </P
><P
>        Each job is one process or one thread, depending on the
        operating system, and uses a separate connection to the
        server.
       </P
><P
>        The optimal value for this option depends on the hardware
        setup of the server, of the client, and of the network.
        Factors include the number of CPU cores and the disk setup.  A
        good place to start is the number of CPU cores on the server,
        but values larger than that can also lead to faster restore
        times in many cases.  Of course, values that are too high will
        lead to decreased performance because of thrashing.
       </P
><P
>        Only the custom archive format is supported with this option.
        The input file must be a regular file (not, for example, a
        pipe).  This option is ignored when emitting a script rather
        than connecting directly to a database server.  Also, multiple
        jobs cannot be used together with the
        option <TT
CLASS="OPTION"
>--single-transaction</TT
>.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-l</TT
><BR><TT
CLASS="OPTION"
>--list</TT
></DT
><DD
><P
>        List the contents of the archive. The output of this operation
        can be used as input to the <TT
CLASS="OPTION"
>-L</TT
> option.  Note that
        if filtering switches such as <TT
CLASS="OPTION"
>-n</TT
> or <TT
CLASS="OPTION"
>-t</TT
> are
        used with <TT
CLASS="OPTION"
>-l</TT
>, they will restrict the items listed.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-L <TT
CLASS="REPLACEABLE"
><I
>list-file</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--use-list=<TT
CLASS="REPLACEABLE"
><I
>list-file</I
></TT
></TT
></DT
><DD
><P
>        Restore only those archive elements that are listed in <TT
CLASS="REPLACEABLE"
><I
>list-file</I
></TT
>, and restore them in the
        order they appear in the file.  Note that
        if filtering switches such as <TT
CLASS="OPTION"
>-n</TT
> or <TT
CLASS="OPTION"
>-t</TT
> are
        used with <TT
CLASS="OPTION"
>-L</TT
>, they will further restrict the items restored.
       </P
><P
><TT
CLASS="REPLACEABLE"
><I
>list-file</I
></TT
> is normally created by
        editing the output of a previous <TT
CLASS="OPTION"
>-l</TT
> operation.
        Lines can be moved or removed, and can also
        be commented out by placing a semicolon (<TT
CLASS="LITERAL"
>;</TT
>) at the
        start of the line.  See below for examples.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-n <TT
CLASS="REPLACEABLE"
><I
>namespace</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--schema=<TT
CLASS="REPLACEABLE"
><I
>schema</I
></TT
></TT
></DT
><DD
><P
>        Restore only objects that are in the named schema.  This can be
        combined with the <TT
CLASS="OPTION"
>-t</TT
> option to restore just a
        specific table.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-O</TT
><BR><TT
CLASS="OPTION"
>--no-owner</TT
></DT
><DD
><P
>        Do not output commands to set
        ownership of objects to match the original database.
        By default, <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> issues
        <TT
CLASS="COMMAND"
>ALTER OWNER</TT
> or
        <TT
CLASS="COMMAND"
>SET SESSION AUTHORIZATION</TT
>
        statements to set ownership of created schema elements.
        These statements will fail unless the initial connection to the
        database is made by a superuser
        (or the same user that owns all of the objects in the script).
        With <TT
CLASS="OPTION"
>-O</TT
>, any user name can be used for the
        initial connection, and this user will own all the created objects.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-P <TT
CLASS="REPLACEABLE"
><I
>function-name(argtype [, ...])</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--function=<TT
CLASS="REPLACEABLE"
><I
>function-name(argtype [, ...])</I
></TT
></TT
></DT
><DD
><P
>        Restore the named function only.  Be careful to spell the function
        name and arguments exactly as they appear in the dump file's table
        of contents.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-R</TT
><BR><TT
CLASS="OPTION"
>--no-reconnect</TT
></DT
><DD
><P
>        This option is obsolete but still accepted for backwards
        compatibility.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-s</TT
><BR><TT
CLASS="OPTION"
>--schema-only</TT
></DT
><DD
><P
>        Restore only the schema (data definitions), not the data (table
        contents).  Current sequence values will not be restored, either.
        (Do not confuse this with the <TT
CLASS="OPTION"
>--schema</TT
> option, which
        uses the word <SPAN
CLASS="QUOTE"
>"schema"</SPAN
> in a different meaning.)
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-S <TT
CLASS="REPLACEABLE"
><I
>username</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--superuser=<TT
CLASS="REPLACEABLE"
><I
>username</I
></TT
></TT
></DT
><DD
><P
>        Specify the superuser user name to use when disabling triggers.
        This is only relevant if <TT
CLASS="OPTION"
>--disable-triggers</TT
> is used.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-t <TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--table=<TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
></TT
></DT
><DD
><P
>        Restore definition and/or data of named table only.  This can be
        combined with the <TT
CLASS="OPTION"
>-n</TT
> option to specify a schema.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-T <TT
CLASS="REPLACEABLE"
><I
>trigger</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--trigger=<TT
CLASS="REPLACEABLE"
><I
>trigger</I
></TT
></TT
></DT
><DD
><P
>        Restore named trigger only.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-v</TT
><BR><TT
CLASS="OPTION"
>--verbose</TT
></DT
><DD
><P
>        Specifies verbose mode.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-V</TT
><BR><TT
CLASS="OPTION"
>--version</TT
></DT
><DD
><P
>       Print the <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> version and exit.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-x</TT
><BR><TT
CLASS="OPTION"
>--no-privileges</TT
><BR><TT
CLASS="OPTION"
>--no-acl</TT
></DT
><DD
><P
>        Prevent restoration of access privileges (grant/revoke commands).
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-1</TT
><BR><TT
CLASS="OPTION"
>--single-transaction</TT
></DT
><DD
><P
>        Execute the restore as a single transaction (that is, wrap the
        emitted commands in <TT
CLASS="COMMAND"
>BEGIN</TT
>/<TT
CLASS="COMMAND"
>COMMIT</TT
>).  This
        ensures that either all the commands complete successfully, or no
        changes are applied. This option implies
        <TT
CLASS="OPTION"
>--exit-on-error</TT
>.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>--disable-triggers</TT
></DT
><DD
><P
>        This option is only relevant when performing a data-only restore.
        It instructs <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> to execute commands
        to temporarily disable triggers on the target tables while
        the data is reloaded.  Use this if you have referential
        integrity checks or other triggers on the tables that you
        do not want to invoke during data reload.
       </P
><P
>        Presently, the commands emitted for
        <TT
CLASS="OPTION"
>--disable-triggers</TT
> must be done as superuser.  So, you
        should also specify a superuser name with <TT
CLASS="OPTION"
>-S</TT
>, or
        preferably run <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> as a
        <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> superuser.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>--no-data-for-failed-tables</TT
></DT
><DD
><P
>        By default, table data is restored even if the creation command
        for the table failed (e.g., because it already exists).
        With this option, data for such a table is skipped.
        This behavior is useful if the target database already
        contains the desired table contents.  For example,
        auxiliary tables for <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> extensions
        such as <SPAN
CLASS="PRODUCTNAME"
>PostGIS</SPAN
> might already be loaded in
        the target database; specifying this option prevents duplicate
        or obsolete data from being loaded into them.
       </P
><P
>        This option is effective only when restoring directly into a
        database, not when producing SQL script output.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>--no-security-labels</TT
></DT
><DD
><P
>        Do not output commands to restore security labels,
        even if the archive contains them.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>--no-tablespaces</TT
></DT
><DD
><P
>        Do not output commands to select tablespaces.
        With this option, all objects will be created in whichever
        tablespace is the default during restore.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>--use-set-session-authorization</TT
></DT
><DD
><P
>        Output SQL-standard <TT
CLASS="COMMAND"
>SET SESSION AUTHORIZATION</TT
> commands
        instead of <TT
CLASS="COMMAND"
>ALTER OWNER</TT
> commands to determine object
        ownership.  This makes the dump more standards-compatible, but
        depending on the history of the objects in the dump, might not restore
        properly.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-?</TT
><BR><TT
CLASS="OPTION"
>--help</TT
></DT
><DD
><P
>       Show help about <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> command line
       arguments, and exit.
       </P
></DD
></DL
></DIV
><P>
   </P
><P
>    <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> also accepts
    the following command line arguments for connection parameters:

    <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="OPTION"
>-h <TT
CLASS="REPLACEABLE"
><I
>host</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--host=<TT
CLASS="REPLACEABLE"
><I
>host</I
></TT
></TT
></DT
><DD
><P
>        Specifies the host name of the machine on which the server is
        running.  If the value begins with a slash, it is used as the
        directory for the Unix domain socket. The default is taken
        from the <TT
CLASS="ENVAR"
>PGHOST</TT
> environment variable, if set,
        else a Unix domain socket connection is attempted.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-p <TT
CLASS="REPLACEABLE"
><I
>port</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--port=<TT
CLASS="REPLACEABLE"
><I
>port</I
></TT
></TT
></DT
><DD
><P
>        Specifies the TCP port or local Unix domain socket file
        extension on which the server is listening for connections.
        Defaults to the <TT
CLASS="ENVAR"
>PGPORT</TT
> environment variable, if
        set, or a compiled-in default.
        </P
></DD
><DT
><TT
CLASS="OPTION"
>-U <TT
CLASS="REPLACEABLE"
><I
>username</I
></TT
></TT
><BR><TT
CLASS="OPTION"
>--username=<TT
CLASS="REPLACEABLE"
><I
>username</I
></TT
></TT
></DT
><DD
><P
>        User name to connect as.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-w</TT
><BR><TT
CLASS="OPTION"
>--no-password</TT
></DT
><DD
><P
>        Never issue a password prompt.  If the server requires
        password authentication and a password is not available by
        other means such as a <TT
CLASS="FILENAME"
>.pgpass</TT
> file, the
        connection attempt will fail.  This option can be useful in
        batch jobs and scripts where no user is present to enter a
        password.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-W</TT
><BR><TT
CLASS="OPTION"
>--password</TT
></DT
><DD
><P
>        Force <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> to prompt for a
        password before connecting to a database.
       </P
><P
>        This option is never essential, since
        <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> will automatically prompt
        for a password if the server demands password authentication.
        However, <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> will waste a
        connection attempt finding out that the server wants a password.
        In some cases it is worth typing <TT
CLASS="OPTION"
>-W</TT
> to avoid the extra
        connection attempt.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>--role=<TT
CLASS="REPLACEABLE"
><I
>rolename</I
></TT
></TT
></DT
><DD
><P
>        Specifies a role name to be used to perform the restore.
        This option causes <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> to issue a
        <TT
CLASS="COMMAND"
>SET ROLE</TT
> <TT
CLASS="REPLACEABLE"
><I
>rolename</I
></TT
>
        command after connecting to the database. It is useful when the
        authenticated user (specified by <TT
CLASS="OPTION"
>-U</TT
>) lacks privileges
        needed by <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
>, but can switch to a role with
        the required rights.  Some installations have a policy against
        logging in directly as a superuser, and use of this option allows
        restores to be performed without violating the policy.
       </P
></DD
></DL
></DIV
><P>
   </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN80030"
></A
><H2
>Environment</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="ENVAR"
>PGHOST</TT
><BR><TT
CLASS="ENVAR"
>PGOPTIONS</TT
><BR><TT
CLASS="ENVAR"
>PGPORT</TT
><BR><TT
CLASS="ENVAR"
>PGUSER</TT
></DT
><DD
><P
>      Default connection parameters
     </P
></DD
></DL
></DIV
><P
>   This utility, like most other <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> utilities,
   also uses the environment variables supported by <SPAN
CLASS="APPLICATION"
>libpq</SPAN
>
   (see <A
HREF="libpq-envars.html"
>Section 31.13</A
>).
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="APP-PGRESTORE-DIAGNOSTICS"
></A
><H2
>Diagnostics</H2
><P
>   When a direct database connection is specified using the
   <TT
CLASS="OPTION"
>-d</TT
> option, <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
>
   internally executes <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> statements. If you have
   problems running <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
>, make sure
   you are able to select information from the database using, for
   example, <A
HREF="app-psql.html"
><SPAN
CLASS="APPLICATION"
>psql</SPAN
></A
>.  Also, any default connection
   settings and environment variables used by the
   <SPAN
CLASS="APPLICATION"
>libpq</SPAN
> front-end library will apply.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="APP-PGRESTORE-NOTES"
></A
><H2
>Notes</H2
><P
>   If your installation has any local additions to the
   <TT
CLASS="LITERAL"
>template1</TT
> database, be careful to load the output of
   <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> into a truly empty database;
   otherwise you are likely to get errors due to duplicate definitions
   of the added objects.  To make an empty database without any local
   additions, copy from <TT
CLASS="LITERAL"
>template0</TT
> not <TT
CLASS="LITERAL"
>template1</TT
>, for example:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE DATABASE foo WITH TEMPLATE template0;</PRE
><P>
  </P
><P
>   The limitations of <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> are detailed below.

   <P
></P
></P><UL
><LI
><P
>      When restoring data to a pre-existing table and the option
      <TT
CLASS="OPTION"
>--disable-triggers</TT
> is used,
      <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> emits commands
      to disable triggers on user tables before inserting the data, then emits commands to
      re-enable them after the data has been inserted.  If the restore is stopped in the
      middle, the system catalogs might be left in the wrong state.
     </P
></LI
><LI
><P
><SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> cannot restore large objects
      selectively;  for instance, only those for a specific table.  If
      an archive contains large objects, then all large objects will be
      restored, or none of them if they are excluded via <TT
CLASS="OPTION"
>-L</TT
>,
      <TT
CLASS="OPTION"
>-t</TT
>, or other options.
     </P
></LI
></UL
><P>
  </P
><P
>   See also the <A
HREF="app-pgdump.html"
>pg_dump</A
> documentation for details on
   limitations of <SPAN
CLASS="APPLICATION"
>pg_dump</SPAN
>.
  </P
><P
>   Once restored, it is wise to run <TT
CLASS="COMMAND"
>ANALYZE</TT
> on each
   restored table so the optimizer has useful statistics; see
   <A
HREF="routine-vacuuming.html#VACUUM-FOR-STATISTICS"
>Section 23.1.3</A
> and
   <A
HREF="routine-vacuuming.html#AUTOVACUUM"
>Section 23.1.5</A
> for more information.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="APP-PGRESTORE-EXAMPLES"
></A
><H2
>Examples</H2
><P
>   Assume we have dumped a database called <TT
CLASS="LITERAL"
>mydb</TT
> into a
   custom-format dump file:

</P><PRE
CLASS="SCREEN"
><SAMP
CLASS="PROMPT"
>$</SAMP
> <KBD
CLASS="USERINPUT"
>pg_dump -Fc mydb &gt; db.dump</KBD
></PRE
><P>
  </P
><P
>   To drop the database and recreate it from the dump:

</P><PRE
CLASS="SCREEN"
><SAMP
CLASS="PROMPT"
>$</SAMP
> <KBD
CLASS="USERINPUT"
>dropdb mydb</KBD
>
<SAMP
CLASS="PROMPT"
>$</SAMP
> <KBD
CLASS="USERINPUT"
>pg_restore -C -d postgres db.dump</KBD
></PRE
><P>

   The database named in the <TT
CLASS="OPTION"
>-d</TT
> switch can be any database existing
   in the cluster; <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> only uses it to issue the
   <TT
CLASS="COMMAND"
>CREATE DATABASE</TT
> command for <TT
CLASS="LITERAL"
>mydb</TT
>.  With
   <TT
CLASS="OPTION"
>-C</TT
>, data is always restored into the database name that appears
   in the dump file.
  </P
><P
>   To reload the dump into a new database called <TT
CLASS="LITERAL"
>newdb</TT
>:

</P><PRE
CLASS="SCREEN"
><SAMP
CLASS="PROMPT"
>$</SAMP
> <KBD
CLASS="USERINPUT"
>createdb -T template0 newdb</KBD
>
<SAMP
CLASS="PROMPT"
>$</SAMP
> <KBD
CLASS="USERINPUT"
>pg_restore -d newdb db.dump</KBD
></PRE
><P>

   Notice we don't use <TT
CLASS="OPTION"
>-C</TT
>, and instead connect directly to the
   database to be restored into.  Also note that we clone the new database
   from <TT
CLASS="LITERAL"
>template0</TT
> not <TT
CLASS="LITERAL"
>template1</TT
>, to ensure it is
   initially empty.
  </P
><P
>   To reorder database items, it is first necessary to dump the table of
   contents of the archive:
</P><PRE
CLASS="SCREEN"
><SAMP
CLASS="PROMPT"
>$</SAMP
> <KBD
CLASS="USERINPUT"
>pg_restore -l db.dump &gt; db.list</KBD
></PRE
><P>
   The listing file consists of a header and one line for each item, e.g.:
</P><PRE
CLASS="PROGRAMLISTING"
>;
; Archive created at Mon Sep 14 13:55:39 2009
;     dbname: DBDEMOS
;     TOC Entries: 81
;     Compression: 9
;     Dump Version: 1.10-0
;     Format: CUSTOM
;     Integer: 4 bytes
;     Offset: 8 bytes
;     Dumped from database version: 8.3.5
;     Dumped by pg_dump version: 8.3.8
;
;
; Selected TOC Entries:
;
3; 2615 2200 SCHEMA - public pasha
1861; 0 0 COMMENT - SCHEMA public pasha
1862; 0 0 ACL - public pasha
317; 1247 17715 TYPE public composite pasha
319; 1247 25899 DOMAIN public domain0 pasha</PRE
><P>
   Semicolons start a comment, and the numbers at the start of lines refer to the
   internal archive ID assigned to each item.
  </P
><P
>   Lines in the file can be commented out, deleted, and reordered. For example:
</P><PRE
CLASS="PROGRAMLISTING"
>10; 145433 TABLE map_resolutions postgres
;2; 145344 TABLE species postgres
;4; 145359 TABLE nt_header postgres
6; 145402 TABLE species_records postgres
;8; 145416 TABLE ss_old postgres</PRE
><P>
   could be used as input to <SPAN
CLASS="APPLICATION"
>pg_restore</SPAN
> and would only restore
   items 10 and 6, in that order:
</P><PRE
CLASS="SCREEN"
><SAMP
CLASS="PROMPT"
>$</SAMP
> <KBD
CLASS="USERINPUT"
>pg_restore -L db.list db.dump</KBD
></PRE
><P></P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN80123"
></A
><H2
>See Also</H2
><A
HREF="app-pgdump.html"
>pg_dump</A
>, <A
HREF="app-pg-dumpall.html"
><SPAN
CLASS="APPLICATION"
>pg_dumpall</SPAN
></A
>, <A
HREF="app-psql.html"
><SPAN
CLASS="APPLICATION"
>psql</SPAN
></A
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="app-pg-dumpall.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="app-psql.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><SPAN
CLASS="APPLICATION"
>pg_dumpall</SPAN
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="reference-client.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><SPAN
CLASS="APPLICATION"
>psql</SPAN
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>